package com.bulletelc.apollopre.module.cases;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.CaseListBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by Administrator on 2018/5/16.
 */

public class CaseListPresenter extends BasePresenter<CaseListContract.View> implements CaseListContract.Persenter {

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void getCaseList() {
        getIntentCaseListImp();
    }

    private void getIntentCaseListImp() {
        OkGo.<String>get(NetWorkPath.GET_CASE_LIST)
                .params("access_token", MyApplication.getToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseCaseList(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorCaseList(response.getException().toString());
                        }
                    }
                });

    }

    private void parseCaseList(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
                        @Override
                        public boolean shouldSkipField(FieldAttributes f) {
                            return f.getName().contains("_links");
                        }

                        @Override
                        public boolean shouldSkipClass(Class<?> clazz) {
                            return false;
                        }
                    }).create();
                    Type type = new TypeToken<List<CaseListBean>>(){}.getType();
                    List<CaseListBean> caseListBeanList = gson.fromJson(object.getJSONArray("data").toString(), type);
                    if (isViewAttached()) {
                        getView().onSuccessCaseList(caseListBeanList);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailCaseList(meta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailCaseList(response.body().toString());
            }
        }
    }


}
